import {inject, observer} from "mobx-react";
import {Modal, Button} from "antd";
import {WeaLocaleProvider, WeaDialog} from "ecCom";
import * as authTypes from "../../constants/secondAuthType";

const getLabel = WeaLocaleProvider.getLabel;

export default class Main extends React.Component {
    constructor(props) {
        super(props);

        this.state = {errorMsg: ""};
    }

    render() {
        const {visible, secondAuthStore} = this.props;
        const {secondAuthType, signUrl = ""} = secondAuthStore;
        const title = getLabel(501195, "身份验证");
        const dialogWidth = 520;
        const dialogHeight = 500;
        const style = {width: dialogWidth, height: dialogHeight};
        const onCancel = this.closeDialog;
        const buttons = [];
        const dialogParams = {title, style, visible, onCancel, buttons};
        dialogParams.icon = "icon-coms-workflow";
        dialogParams.iconBgcolor = '#0079DE';
        if (!!signUrl) {
            dialogParams.url = signUrl;
        }
        return <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@i0y51f`} {...dialogParams}/>
    }

    componentDidMount() {
        this.resisterListener();
    }

    componentWillUnMount() {
        this.removeListener();
    }

    shouldComponentUpdate(nextProps, nextState, nextContext) {
        if (this.state.errorMsg != nextState.errorMsg) {
            if (!!nextState.errorMsg) {
                Modal.error({content: nextState.errorMsg});
                this.setState({errorMsg: ""});
            }
        }

        return true;
    }

    resisterListener() {
        //为窗口注册message事件，并绑定监听函数
        if (window.addEventListener) {
            window.addEventListener('message', this.receiveMessage, false);
        } else {
            window.attachEvent('message', this.receiveMessage);
        }
    }

    removeListener() {
        //为窗口注册message事件，并绑定监听函数
        if (window.removeEventListener) {
            window.removeEventListener('message', this.receiveMessage, false);
        } else {
            window.detachEvent('message', this.receiveMessage);
        }
    }

    receiveMessage = (msg)  => {
        const {secondAuthStore} = this.props;
        const {secondAuthType, isProtectDialog, signUrl = ""} = secondAuthStore;
        let data = msg.data || {};
        if (typeof data == "string") {
            data = JSON.parse(data);
        }

        const origin = msg.origin;
        if (!!origin && signUrl.startsWith(origin)) {  //说明是从契约锁发送的消息
            const {action = ""} = data;
            const qysData = data.data || {};

            if (action == "cancel") {        //契约锁窗口，点击取消操作
                if (this.props.onClose) {
                    this.props.onClose();
                }
            } else if (action == "complete") {      //签署完成
                if (this.props.onSuccess) {
                    this.props.onSuccess({secondAuthType, isProtectDialog, ...qysData});
                }
            } else if (action == "message") {
                const qysMsg = qysData.message;
                this.setState({errorMsg: qysMsg});
            }
        }
    }

    //关闭
    closeDialog = () =>  {
        if (this.props.onClose) {
            this.props.onClose();
        }
    }
}